package com.zuihai.app.mvp.ui.activity.base;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatEditText;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.mvp.IPresenter;
import com.zuihai.app.R;
import com.zuihai.app.mvp.ui.widget.ToolbarWrapper;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/10 0010.
 */

public abstract class ToolBarBaseSearchFragment<P extends IPresenter> extends BaseFragment<P> {
    @BindView(R.id.toolbar)
    ToolbarWrapper toolbar;

    @BindView(R.id.fl_title)
    FrameLayout frameLayout;
    @BindView(R.id.iv_top_back)
    ImageView ivTopBack;
    @BindView(R.id.iv_top_search)
    ImageView ivTopSearch;
    @BindView(R.id.iv_top_edit)
    ImageView ivTopEdit;
    @BindView(R.id.et_search)
    AppCompatEditText etSearch;
    @BindView(R.id.tv_tobar_title_text)
    TextView mTitle;

    @BindView(R.id.iv_search_delete)
    ImageView ivSearchDelete;
    public ImageView getSearch(){
        return ivTopSearch;
    }

    public ImageView getImageDelete(){
        return ivSearchDelete;
    }

    public ImageView getImageEdit(){
        return ivTopEdit;
    }

    public AppCompatEditText getEditText(){
        return etSearch;
    }

    public ImageView getBack(){
        return ivTopBack;
    }

    @Override
    protected void initData() {
        initToolbar();
    }

    /**
     * 初始化Toolbar布局
     */
    private void initToolbar() {
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ivTopBack.setVisibility(hasBack() ? View.VISIBLE : View.GONE);
        ivTopBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.e("点击了返回了啊");
                getActivity().onBackPressed();
            }
        });
    }

    @Override
    protected final View initView(LayoutInflater inflater, ViewGroup container) {
        //获取ToolbarLayout
        ViewGroup toolbarLayout = (ViewGroup) LayoutInflater.from(getContext())
                .inflate(R.layout.activity_top_bar_base_search, null, false);
        //找到内容容器
        ViewGroup contentContainer = (ViewGroup) toolbarLayout.findViewById(R.id.fl_title);
        //将内容放入到容器中
        LayoutInflater.from(getContext()).inflate(initContentView(), contentContainer, true);
        return toolbarLayout;
    }

    protected abstract int initContentView();

    public boolean hasBack() {
        return true;
    }

    public ToolbarWrapper getToolbar() {
        return toolbar;
    }

    @OnClick(R.id.iv_top_back)
    public void onBack(View v) {
        onBackPressedSupport();
    }

    public void setTitle(String title){
        mTitle.setText(title);
    }


}
